//
//  ZJPushInfoManager.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/27.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJPushInfoManager.h"
#import "ZJSqlit.h"
@interface ZJPushInfoManager ()
/** push数据表 */
@property (nonatomic, strong) ZJSqlit *sqlit;
@end

@implementation ZJPushInfoManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self sqlit];
    }
    return self;
}

+ (ZJPushInfoManager *)sharedPushInfo{
    static ZJPushInfoManager *pushInfo = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pushInfo = [[ZJPushInfoManager alloc]init];
    });
    return pushInfo;
}


- (void)addPushMessage:(NSDictionary *)message{
    ZJPushInfoModel *messageModel = [ZJPushInfoModel yy_modelWithJSON:message];
    if (!messageModel.ID) {
        return;
    }
    NSMutableArray *array = [self readPushInfo];
    NSMutableArray *array_Model = [[NSArray yy_modelArrayWithClass:[ZJPushInfoModel class] json:array] mutableCopy];
    __block NSInteger flag = 0;
    __block NSString *index = nil;
    [array_Model enumerateObjectsUsingBlock:^(ZJPushInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.ID isEqualToString:messageModel.ID]) {
            flag = 1;
            index = [NSString stringWithFormat:@"%ld",(unsigned long)idx];
        }
    }];
    if (flag == 0) {//原来没有
        [array_Model addObject:messageModel];
    }else{//原来有
        if (index) {
            array_Model[[index integerValue]] = messageModel;
        }
    }
      [self.sqlit.keyValueStore putString:[array_Model yy_modelToJSONString] withId:INFO_ACCOUNT intoTable:PUSH_INFO];
}

- (void)removePushMessageWithID:(NSString *)ID{
    NSMutableArray *array = [self readPushInfo];
    if (array.count > 0) {
         NSMutableArray *array_Model = [[NSArray yy_modelArrayWithClass:[ZJPushInfoModel class] json:array] mutableCopy];
        __block ZJPushInfoModel *removeModel = nil;
        [array_Model enumerateObjectsUsingBlock:^(ZJPushInfoModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.ID isEqualToString:ID]) {
                removeModel = obj;
            }
        }];
        if (removeModel) {
            [array_Model removeObject:removeModel];
        }
        [self.sqlit.keyValueStore putString:[array_Model yy_modelToJSONString] withId:INFO_ACCOUNT intoTable:PUSH_INFO];
    }
}

- (NSInteger)unReadMessageNumber{
    NSMutableArray *array = [self readPushInfo];
    NSMutableArray *array_Model = [[NSArray yy_modelArrayWithClass:[ZJPushInfoModel class] json:array] mutableCopy];
    __block NSInteger unReadNumber = 0;
    [array_Model enumerateObjectsUsingBlock:^(ZJPushInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        /** 是否阅读  0、否 1、是 */
        if (![obj.state isEqualToString:@"1"]) {
            unReadNumber ++;
        }
    }];
    return unReadNumber;
}

- (NSMutableArray *)readPushInfo{
    NSString * string = [self.sqlit.keyValueStore getStringById:INFO_ACCOUNT fromTable:PUSH_INFO];
    
    
    if (!string) {
        return   [NSMutableArray array];
    }else{
    return [self jsonStringToObject:string];
    }
    
}



- (ZJSqlit *)sqlit{
    if (_sqlit == nil){
        _sqlit = [[ZJSqlit alloc]init];
        [_sqlit createTableWithName:PUSH_INFO];
    }
    return _sqlit;
}

@end


@implementation ZJPushInfoModel

- (NSString *)description{
    return [self yy_modelDescription];
}

+ (NSDictionary *)modelCustomPropertyMapper{
    
    return @{@"ID":@"id"};
}

- (NSDictionary *)dataDict{
    if (self.data.length > 0) {
        NSString *dictString = [self.data substringFromIndex:1];
        NSDictionary * dict = [self jsonStringToObject:dictString];
        
        return dict;
    }
    return nil;
}

- (CGFloat)cellHeight{
    CGFloat ImageH = 0.0;
    if (self.cover) {//封面存在
        ImageH = 200 + 5.0;
    }
    CGFloat contentLabelH =  [self.desc getLabelSizeWithLabelFont:[UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]] labelWidth:[UIScreen zj_screenWith] - 24 * [UIScreen zj_screenWScale] - 20 * [UIScreen zj_screenHScale] labelNumberOfLines:0 labelLineSpace:2 * [UIScreen zj_screenHScale] labelCharacterSpace:1];
    
    return (10 + 10 + 40 + 5 + 5 + 3  + 10) * [UIScreen zj_screenHScale] + ImageH + contentLabelH + 1 + [NSString heightWithTextFont:[UIFont subTitleTextFont]];
}

@end
